// ActionScript file
import com.helloniu.controller.ChatController;
import com.helloniu.controller.MainPanelController;
import com.helloniu.controller.UserListController;
import com.helloniu.controller.WhiteBoardController;
import com.helloniu.fl.NCHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.util.StyleTool;
import com.helloniu.fl.util.Tool;
import com.helloniu.model.MainPanelModel;
//import com.helloniu.view.WBWindow;

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import mx.events.FlexEvent;
import mx.managers.PopUpManager;

private var connUrl0:String = "rtmp://";
private var connUrl:String = "rtmp://localhost/SOSample/12344";
private var ncHandler:NCHandler = null;
private var soHandler:SOHandler = null;
private var userSO:String = "UsersSO";
private var _userID:String = null;

private function initApp(e:*):void {
    userID.enabled = true;

    StyleTool.setBtnStyle(connectBtn, "connectIcon");
    StyleTool.setBtnStyle(statusBtn, "statusCancelIcon");
    StyleTool.setBtnStyle(syncBtn, "syncIcon");


    Tool.setDebugArea(consoleTxt);

    MainPanelController.getIns().addView(this);
}

private function userIDKeyUp(evt:KeyboardEvent):void {
    if(evt.keyCode == Keyboard.ENTER) {
        connectToRed5();
    }
}

private function connectToRed5(e:* = null):void {
    _userID = userID.text;

    if(_userID == null || _userID == "") {
        Tool.alert("null");
        return;
    }

    var n:int = parseInt(confID.text);

    if(isNaN(n)) {
        Tool.alert("null");
        return;
    }

    if(ncHandler != null && ncHandler.getNC().connected) {
        ncHandler.getNC().close();
        //-closed-
        userID.enabled = true;
        StyleTool.setBtnStyle(connectBtn, "connectIcon");
        StyleTool.setBtnStyle(statusBtn, "statusCancelIcon");
        return;
    }

    ncHandler = new NCHandler();
    soHandler = new SOHandler();
    ncHandler.setURL(connUrl0 + ipID.text + "/SOSample/" + confID.text);
    ncHandler.connect(connectSuccess);
    ncHandler.addClosedListener(connectionClosed);
}

private function connectionClosed(handler:NCHandler):void {
    Tool.alert("sss");
    handler.disconnect();
}

private function connectSuccess(handler:NCHandler):void {
    soHandler.setName(userSO);
    soHandler.setNC(handler.getNC());
    soHandler.setConnectSuccessListener(soConnected);
    soHandler.connect();

    // reset the status of the app
    userID.enabled = false;
    StyleTool.setBtnStyle(connectBtn, "disconnectIcon");
    StyleTool.setBtnStyle(statusBtn, "statusOKIcon");
}

private function soConnected(asoHandler:SOHandler):void {
    UserListController.getIns().initConnection(ncHandler, soHandler, _userID);
    ChatController.getIns().initConnection(ncHandler, soHandler, _userID);
    WhiteBoardController.getIns().initConnection(ncHandler, soHandler, _userID);

    MainPanelController.getIns().initConnection(ncHandler, soHandler, _userID);
}

private function syncPanel(event:*):void {
    MainPanelController.getIns().syncMainPanel();
}

private function tabPanelChanged(event:*):void {
    oldInfo = shareComp.selectedIndex;
    (MainPanelController.getIns().getModel() as MainPanelModel).
        setSelectedIndex(oldInfo);
}

private var oldInfo:int = 0;
public function updateModel():void {
    var model:MainPanelModel = MainPanelController.getIns().getModel() as MainPanelModel;
    var newInfo:int = model.getSelectedIndex()

    // users does not chnage
    if(oldInfo == newInfo) {
        return;
    }

    oldInfo = newInfo;
    populate();
}

private function populate():void {
    shareComp.selectedIndex = oldInfo;
}

private function fullScreenToggle(e:*):void {
        stage.displayState =
            stage.displayState == 'fullScreen' ? 'normal' : 'fullScreen';
}

private function createComplete(e:FlexEvent):void {
    /*
    var window:WBWindow=e.target as WBWindow;
    window.initAPP();
    */
}

private var wbnameIdx:int = 0;
private function createWB(e:* = null):void {
    /*
    var window:WBWindow=new WBWindow();
    window.width = 400;
    window.height = 400;
    var wbid:String = "wbname" + wbnameIdx;
    window.name = wbid;
    window.title = wbid;
    window.setID(wbid);
    wbnameIdx = wbnameIdx + 1;

    window.addEventListener(FlexEvent.CREATION_COMPLETE, createComplete);
    PopUpManager.addPopUp(window, this, false);
    PopUpManager.centerPopUp(window);
    */
}